//
//  OpinionView.m
//  JPKY
//
//  Created by 吴俊杰 on 2016/11/15.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "OpinionView.h"

@interface OpinionView () <UITextViewDelegate>

// 提示字label
@property (weak, nonatomic) UILabel *placeHolderLabel;
// 字数统计label
@property (weak, nonatomic) UILabel *countLabel;

@end

@implementation OpinionView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    
    // 意见输入框
    UITextView *inputTextView = [[UITextView alloc] initWithFrame:self.bounds];
    [self addSubview:inputTextView];
    inputTextView.font = [UIFont systemFontOfSize:16.0];
    inputTextView.delegate = self;
    
    // 输入提示框
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    [self addSubview:placeHolderLabel];
    self.placeHolderLabel = placeHolderLabel;
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.text = @"请输入你的意见最多140字";
    placeHolderLabel.textColor = [UIColor grayColor];
    
    [placeHolderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(inputTextView).offset(5);
        make.top.mas_equalTo(inputTextView).offset(66);
        make.width.equalTo(self);
        make.height.mas_equalTo(30);
    }];
    
    // 字数统计框
    UILabel *countLabel = [[UILabel alloc]init];
    self.countLabel = countLabel;
    [self addSubview:countLabel];
    countLabel.text = @"140/140";
    countLabel.textColor = [UIColor lightGrayColor];
    
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.bottom.mas_equalTo(self).offset(-5);
    }];
}

#pragma mark - UITextViewDelegate 代理方法
- (void)textViewDidChange:(UITextView *)textView {
    
    self.placeHolderLabel.hidden = !(textView.text.length == 0);
    self.countLabel.text = [NSString stringWithFormat:@"%zd/140",140 - textView.text.length];
}

@end
